<?php
namespace app\compent\controller;
use app\common\Emodel;
use think\Controller;
use think\Db;

class Util extends Controller {
    
    public function organizeDepart() {
		$sql = "select od.ID, pt.DEPART, od.PARENT_ID, od.POSITION 
		  	  from  T_ORGANIZE_DEPART od
			  inner join (
				select ID, DEPART from T_ORGANIZE_DEPART where PARENT_ID = 0
			  ) pt on od.PARENT_ID = pt.ID
			  where od.PARENT_ID > 0
			  order by weight";
		$Emodel = new Emodel();
		return $Emodel->query($sql);
	}

	public function gradeList() {
		$list = gradeList();
		$list[count($list)]=array("text"=>"其它","value"=>"0");
		return $list;
	}

	public function subjectList(){
		return subjectList();
	}


	public function teacherList() {
		$grade = input("grade");
		$subjectId = input("subjectId");
		$key = input("q");
		$where = [];
		if($key) {
			$where["姓名"] = array("like", $key);
		}
		if($grade) {
			$array = explode("_", $grade);
			$grade = $array[0];
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($subjectId) $where["任教学科ID"] = $subjectId;
		$result = Db::table("教师信息")
			->field("ID id, 姓名 name")
			->where($where)
			->order("name")
			->select();
		$list[0]=array("text"=>"请选择", "value"=>"");
		foreach($result as $key => $obj){
			$list[intval($key) + 1] = array("text"=>$obj["name"], "value"=>$obj["id"]);
	    }
	    return $list;
	}

	public function clazzList() {
		$grade = input("grade");
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		$data = Db::table("学生信息")
			   ->field("班号 clazz")
			   ->where($where)
			   ->order("clazz")
			   ->group("班号")
			   ->select();
		$list[0]=array("text"=>"请选择", "value"=>"");
		foreach($data as $key => $obj){
			$list[intval($key) + 1] = array("text"=>$obj["clazz"]."班", "value"=>$obj["clazz"]);
	    }
	    return $list;
	}

	public function studentList() {
		$grade = input("grade");
		$clazz = input("clazz");
		if($grade) {
			$array = explode("_", $grade);
			$where["年级号"] = array("eq", $array[0]);
			$where["学籍类型"] = array("eq", $array[1]);
		}
		if($clazz) $where["班号"] = $clazz;
		return Db::table("学生信息")
			   ->field("ID value, 姓名 text")
			   ->where($where)
			   ->select();
	}

	public function termList() {
		$result = Db::table("学期名称")
				  ->order("ID desc")
				  ->select();
		return selectResult($result);
	}

	public function gradeNameList() {
		return Db::table("年级名称")
		       ->field("ID value, 年级名称 text")
		       ->select();
	}

	public function getTermExam(){
		$where["学期ID"] = input("termId");
		$where["年级ID"] = input("grade");
		return Db::table("考试名称")
		       ->field("ID value, 考试名称 text")
		       ->where($where)
		       ->order("value desc")
		       ->select();
	}
}
